package com.hotel.controller;

import com.hotel.factory.BeanFactory;
import com.hotel.pojo.FoodType;
import com.hotel.service.FoodTypeService;
import com.hotel.utils.WebUtils;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * Created by LinYaTing on 2018/11/24 12:35
 */
@WebServlet(name = "FoodTypeServlet", urlPatterns = "/foodType")
public class FoodTypeServlet extends BaseServlet {

    //跳转资源
    private Object uri;

    public Object addFoodType(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
         //获取数据封装数据
            FoodType foodType = new FoodType();
            BeanUtils.populate(foodType, request.getParameterMap());
            foodTypeService.save(foodType);
            uri = request.getRequestDispatcher("/foodType?method=listFoodType");
            return uri;
    }

    //菜系列表展示
    public Object listFoodType(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
           //调用业务逻辑
           List<FoodType> foodTypeList = foodTypeService.findAll();
           //保存
           request.setAttribute("listFoodType", foodTypeList);
           //跳转的菜系列表
           uri = request.getRequestDispatcher("/sys/type/foodType_list.jsp");
           return uri;
    }

    // 进入更新页面
    public Object viewUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
            //获取请求数据封装数据
            String id = request.getParameter("id");
            FoodType ft = foodTypeService.findFoodTypeById(Integer.parseInt(id));
            //保存
            request.setAttribute("foodType", ft);
            uri = request.getRequestDispatcher("/sys/type/foodType_update.jsp");
            return uri;
    }

    // 更新
    public Object update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
            //获取请求数据封装数据
            FoodType foodType = new FoodType();
            BeanUtils.populate(foodType, request.getParameterMap());
            //调用业务逻辑
            foodTypeService.update(foodType);
            uri = "/foodType?method=listFoodType";
            return uri;
    }

    //删除
    public Object delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        try {
            //获取请求数据封装数据
            String id = request.getParameter("id");
            //调用业务逻辑
            foodTypeService.delete(Integer.parseInt(id));
            uri = "/foodType?method=listFoodType";
        } catch (Exception e) {
            e.printStackTrace();
            uri = "/foodType?method=listFoodType";
        }
        return uri;
    }

    // 模糊查询数据
    public Object search(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取请求数据封装数据
        String typeName = request.getParameter("typeName");
        //调用业务逻辑
        List<FoodType> foodTypes = foodTypeService.findList(typeName);
        //保存
        request.setAttribute("listFoodType", foodTypes);
        uri = request.getRequestDispatcher("/sys/type/foodType_list.jsp");
        return uri;
    }
}
